package hw2.jpa.ejb.model;

import hw2.jpa.ejb.enumerator.ProductType;
import hw2.jpa.ejb.strategy.CompleteRecognitionStrategy;
import hw2.jpa.ejb.strategy.RecognitionStrategy;
import hw2.jpa.ejb.strategy.ThreeWayRecognitionStrategy;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Product implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;
	private String name;
	private String type;

	@Transient
	private RecognitionStrategy recognitionStrategy;

	public Product() {

	}

	public Product(String name, String type,
			RecognitionStrategy recognitionStrategy) {
		super();
		this.name = name;
		this.type = type;
		this.recognitionStrategy = recognitionStrategy;
	}

	public static Product newWordProcessor(String name) {
		return new Product(name, ProductType.WORDPROCESSOR.getProductType(),
				new CompleteRecognitionStrategy());
	}

	public static Product newSpreadSheet(String name) {
		return new Product(name, ProductType.SPREADSHEET.getProductType(),
				new ThreeWayRecognitionStrategy(60, 90));
	}

	public static Product newDatabase(String name) {
		return new Product(name, ProductType.DATABASE.getProductType(),
				new ThreeWayRecognitionStrategy(30, 60));
	}

	public void calculateRevenueRecognitions(Contract contract) {
		recognitionStrategy.calculateRevenueRecognitions(contract);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public RecognitionStrategy getRecognitionStrategy() {
		return recognitionStrategy;
	}

	public void setRecognitionStrategy(RecognitionStrategy recognitionStrategy) {
		this.recognitionStrategy = recognitionStrategy;
	}

	@Override
	public String toString() {
		return this.id + ":" + this.name + ":" + this.type;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Product)) {
			return false;
		}
		Product other = (Product) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (type == null) {
			if (other.type != null) {
				return false;
			}
		} else if (!type.equals(other.type)) {
			return false;
		}
		return true;
	}

}
